package com.stylefeng.guns.modular.system.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stylefeng.guns.core.base.controller.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ClusterCommandExecutor.MulitNodeResult;

import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.stylefeng.guns.common.persistence.model.Filemeta;
import com.stylefeng.guns.modular.system.service.IFilemetaService;

/**
 * 文件管理控制器
 *
 * @author fengshuonan
 * @Date 2021-01-17 16:54:41
 */
@Controller
@RequestMapping("/filemeta")
public class FilemetaController extends BaseController {
	
	private static final String DESTDIR = "temporaryFolder";
 
    @Autowired
    private IFilemetaService filemetaService;

    /**
     * 获取文件管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return filemetaService.selectList(null);
    }

    /**
     * 新增文件管理,上传文件是记录文件信息，默认上传到临时文件
     */
    @RequestMapping(value = "/uploadFile")
    @ResponseBody
    public Filemeta uploadFile(@RequestParam("file")MultipartFile files,HttpServletRequest request,HttpServletResponse response) {
       Filemeta filemeta = filemetaService.uploadFile(files,DESTDIR,request,response);
       System.out.println(filemeta);
        return filemeta;
    }

    /**
     * 删除文件管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long filemetaId,@RequestParam String temporaryFileUrl) {
    	System.out.println(filemetaId);
    	System.out.println(temporaryFileUrl);
        filemetaService.deleteById(filemetaId);//删除文件记录
        File file = new File(temporaryFileUrl);
        if(file.exists()){
        	file.delete();
        }
        return SUCCESS_TIP;
    }

   /* @SuppressWarnings("static-access")
	@RequestMapping("/downFile")
	public void downFile(HttpServletRequest request,HttpServletResponse response) {
		
		String fileUrl = request.getParameter("fileUrl");
		String fileName = request.getParameter("fileName");
		try {
			if(!ToolUtil.isEmpty(fileName)) {
				fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
			}
			this.downloadFile(request, response, fileName, fileUrl,"text/html;charset=UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.err.println(fileName);
	}	
    */
    
 /*public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String url, String contenType){
    	
    	if(url.toLowerCase().contains("web-inf")){
    		return;
    	}
    	try {
    		response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
	    	
	    	String root = FilePaths.getSystemRootPath();
	    	fileName = fileName + url.substring(url.lastIndexOf("."));
	    	
	    	long fileLength = new File(root+url).length();

	        response.setContentType(contenType);

	        String header = request.getHeader("User-Agent").toUpperCase();
	        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
	            fileName = URLEncoder.encode(fileName, "utf-8");
	            fileName = fileName.replace("+", "%20");    //IE下载文件名空格变+号问题
	        } else {
	            fileName = new String(fileName.getBytes(), "ISO8859-1");
	        }

	        response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
	        response.setHeader("Content-Length", String.valueOf(fileLength));
	         
    		OutputStream os = response.getOutputStream(); 
    		FileInputStream fis = new FileInputStream(new File(root + url)); 
    		
    		byte[] b = new byte[1024]; 
    		int i = 0; 

    		while ( (i = fis.read(b)) > 0 ) { 
    			os.write(b, 0, i); 
    		} 
    		
    		fis.close(); 
    		os.flush(); 
    		os.close(); 
    	} catch ( Exception e ) { 
    		e.printStackTrace();
    	} 
    	
    }*/
    
}
